﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace carsitem.xiaoshou
{
    public partial class KeepSell : Form
    {
        
        private void BindData(string sql)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SellCars;Integrated Security=True");
            SqlDataAdapter dap = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            //你不会用DBHler嘛
            ds.Clear();
            dap.Fill(ds);
            this.Sell_dgv.AutoGenerateColumns = false;
            this.Sell_dgv.DataSource = ds.Tables[0];
        }
        public KeepSell()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {
            
        }

        private void Select_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=SellCars;Integrated Security=True");
            DateTime time = this.StartTime.Value;
            DateTime shijian = this.OverTime.Value;
            string doer = this.PeopleTb.Text.Trim();
            string bian = this.DingTb.Text.Trim();
            zhengzebiaoda zda = new zhengzebiaoda();
            bool re = zda.IsNumber(doer);
            bool jg = zda.IsNumber(bian);
            if (re == false && doer != "")
            {
                MessageBox.Show("输入数据有误，请重新输入");
                return;
            }
             if (jg == false && bian != "")
            {
                MessageBox.Show("输入数据有误，请重新输入");
                return;
            }
            string sql="";
            if (doer == "" && bian == "")
            {
                sql = string.Format(@"select * from Sell where Selldate between '{0}' and '{1}' ",
                    time, shijian);
            }
            if (doer != "" && bian == "")
            {
                sql = string.Format(@"select * from Sell where Selldate between '{0}' and '{1}' and EmployeeID={2}",
                   time, shijian, doer);
            }
            else if (doer == "" && bian != "")
            {
                sql = string.Format(@"select * from Sell where Selldate between '{0}' and '{1}' and 
            SellID = {2}", time, shijian, bian);
            }
            else if (doer != "" && bian != "")
            {
                sql = string.Format(@"select * from Sell where Selldate between '{0}' and '{1}' and EmployeeID={2} and
            SellID = {3}", time, shijian, doer, bian);
            }
            
            SqlDataAdapter dta = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dta.Fill(ds); 
            DataView dv = ds.Tables[0].DefaultView;
            this.Sell_dgv.DataSource = dv;
        }

        private void KeepSell_Load(object sender, EventArgs e)
        {

        }

        private void ChongzhiBt_Click(object sender, EventArgs e)
        {
            foreach (Control con in this.Controls)//遍历控件类集合
            {
                TextBox text = con as TextBox;//获取TextBox对象
                RadioButton rdo = con as RadioButton;//获取RadioButton对象
                CheckBox che = con as CheckBox;//获取CheckBox对象
                if (text != null)//文本框清空
                {
                    text.Text = "";
                }
                if (che != null)//多选框全不选
                {
                    che.Checked = false;
                }
                
            }
        }

        public void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int id;
            if (this.Sell_dgv.SelectedRows.Count > 0)
            {
                 id = Convert.ToInt32(this.Sell_dgv.SelectedRows[0].Cells["SellID"].Value);
            }
            else
            {
                MessageBox.Show("未选中任何行");
                return;
            }

            string sql = string.Format("delete from Sell  where SellID = '{0}'", id);
            DBHelper db = new DBHelper();
            bool result = db.ExcuteSql(sql);
            if (result)
            {
                MessageBox.Show("删除成功！");
                string sql2 = "select * from Sell ";
                BindData(sql2);
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }

        private void YouJian_Opening(object sender, CancelEventArgs e)
        {

        }

        private void UpdateBt_Click(object sender, EventArgs e)
        {
            try
            {
                int sid = Convert.ToInt32(this.Sell_dgv.SelectedRows[0].Cells["SellID"].Value);
                UpdateSell upsell = new UpdateSell();
                upsell.sellid = sid;
                upsell.ShowDialog();
            }
            catch (Exception)
            {
                MessageBox.Show("文本框不能为空");
            }
        }

        private void Sell_dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.Sell_dgv.Columns[e.ColumnIndex].Name.Equals("EmployeeID"))
            {
                
            }
        }

        private void KeepSell_FormClosed(object sender, FormClosedEventArgs e)
        {
            carsitem.MainInterface.MainInterfaceForm mainInterfaceForm = new carsitem.MainInterface.MainInterfaceForm();//把form1传给form2
            mainInterfaceForm.Show();
            this.Hide();//把自己隐藏了
        }
    }

}
